<?php
namespace app\admin\controller;

use app\admin\common\Base;
use think\exception\ValidateException;

/**
 * 首页轮播管理控制器
 * Class Banner
 * @package app\admin\controller
 */
class Banner extends Base {
    public function index($page=1,$limit=10){
        if (is_post()){
            $where[] = ['is_del','=',0];

            $data = $this->getListData('banner',$where,'sort','asc',$page,$limit);

            return json(['code'=>'suc','msg'=>'数据请求成功','count'=>$data['count'],'data'=>$data['list'],'current_page'=>$page]);
        }
        return $this->fetch();
    }

    public function add(){
        if (is_post()){
            //处理数据
            $data = $this->handleData(input('post.'),'add');
            //验证数据
            $validate = $this->validateData($data);
            if ($validate['code'] == 'suc'){
                $result = db('banner')->insertGetId($data);
                if ($result){
                    insert_system_log('添加首页轮播：' . $result);
                    return json(['code' => 'suc', 'msg' => '添加成功']);
                }else {
                    return json(['code' => 'err', 'msg' => '系统繁忙，请重试']);
                }
            }else{
                return json(['code' => $validate['code'], 'msg' => $validate['msg']]);
            }

        }
        return $this->fetch();
    }

    public function edit($id){
        if (is_post()){
            //处理数据
            $data = $this->handleData(input('post.'),'edit');
            //验证数据
            $validate = $this->validateData($data);

            if ($validate['code'] == 'suc'){
                $result = db('banner')
                    ->where('id',$id)
                    ->data($data)
                    ->update();
                if ($result){
                    $code = 'suc';
                    $msg = '修改成功';
                }else{
                    $code = 'err';
                    $msg = '修改失败';
                }
                return json(['code' => $code, 'msg' => $msg]);
            }else{
                return json(['code' => $validate['code'], 'msg' => $validate['msg']]);
            }
        }
        $info = db('banner')
            ->where('id',$id)
            ->find();
        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * 处理数据
     * @param $data
     * @param $action
     * @return mixed
     */
    private function handleData($data,$action){
        if ($action == 'add'){
            $data['create_time'] = time();
        }

        $data['status'] = empty($data['status']) ? 0 : 1;
        $data['update_time'] = time();

        return $data;
    }

    /**
     * 验证数据
     * @param $data
     * @return array
     */
    private function validateData($data){
        $code = 'suc';
        $msg = '';
        if (empty($data['img_url'])){
            $code = 'err';
            $msg = '请上传图片';
        }

        return ['code' => $code,'msg' => $msg];
    }

    public function del($id){
        $result = db('banner')
            ->where('id',$id)
            ->delete();
        if ($result){
            return json(['code' => 'suc','msg' => '删除成功']);
        }else{
            return json(['code' => 'err','msg' => '删除失败']);

        }
    }

    public function changeStatus($type,$id){
        if (is_post()){
            if ($this->changeRecordStatus('banner',$id,1,$type)){
                if ($type == 1){
                    $log_msg = '显示轮播图：' . $id;
                    $msg = '已显示';
                }else{
                    $log_msg = '隐藏轮播图：' . $id;
                    $msg = '已隐藏';
                }
                insert_system_log($log_msg);
                return json(['code' => 'suc','msg' => $msg]);
            }
            return json(['code' => 'err','msg' => '系统繁忙，请重试']);
        }
    }

    public function upload(){
        $file = request()->file('file');

        $info = $this->uploadsImg($file, 'uploads/banner/');

        if ($info !== false) {
            return json(['code' => 'suc', 'msg' => '图片上传成功', 'url' => $info['path']]);
        } else {
            return json(['code' => 'err', 'msg' => '图片上传失败，请重试']);
        }
    }
}